package cn.bjxy.javaks.store_ms.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import cn.bjxy.javaks.store_ms.entity.AdminEntity;
import cn.bjxy.javaks.store_ms.service.AdminServiceImpl;
import cn.bjxy.javaks.store_ms.service.IAdminService;

public class InforWindow extends JDialog {
	final JTextField nameField, genderField;// 定义三个输入框
	private JPasswordField passwordField;
	private JTextField textField;

	private String framType;
	private JButton modifiy;

	public InforWindow(AdminEntity adminEntity, String framType) {
		this.framType = framType;

		setTitle("个人信息");
		setModal(true);
		setSize(300, 200);// 对话框的大小
		setResizable(false);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setLocationRelativeTo(null);
		JPanel jp = new JPanel();
		changeColor(jp, 2);
		GridBagLayout gbl_jp = new GridBagLayout();
		gbl_jp.columnWidths = new int[] { 130, 127, 0, 0 };
		gbl_jp.rowHeights = new int[] { 30, 30, 30, 30, 30, 0 };
		gbl_jp.columnWeights = new double[] { 0.0, 0.0, 0.0, Double.MIN_VALUE };
		gbl_jp.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		jp.setLayout(gbl_jp);

		JLabel userName = new JLabel("姓名：");
		changeColor(userName, -1);
		GridBagConstraints gbc_jl1 = new GridBagConstraints();
		gbc_jl1.anchor = GridBagConstraints.EAST;
		gbc_jl1.fill = GridBagConstraints.VERTICAL;
		gbc_jl1.gridx = 0;
		gbc_jl1.gridy = 0;
		jp.add(userName, gbc_jl1);

		nameField = new JTextField(8);
		nameField.setCaretColor(Color.WHITE);
		changeColor(nameField, 1);
		nameField.setText(adminEntity.getName());
		GridBagConstraints gbc_jtf1 = new GridBagConstraints();
		gbc_jtf1.fill = GridBagConstraints.HORIZONTAL;
		gbc_jtf1.gridx = 1;
		gbc_jtf1.gridy = 0;
		jp.add(nameField, gbc_jtf1);

		JLabel password = new JLabel("密码：");
		changeColor(password, -1);
		GridBagConstraints gbc_jl2 = new GridBagConstraints();
		gbc_jl2.anchor = GridBagConstraints.EAST;
		gbc_jl2.fill = GridBagConstraints.VERTICAL;
		gbc_jl2.gridx = 0;
		gbc_jl2.gridy = 1;
		jp.add(password, gbc_jl2);

		passwordField = new JPasswordField();
		passwordField.setText(adminEntity.getPassword());
		passwordField.setCaretColor(Color.WHITE);
		changeColor(passwordField, 1);
		GridBagConstraints gbc_jtf2 = new GridBagConstraints();
		gbc_jtf2.fill = GridBagConstraints.HORIZONTAL;
		gbc_jtf2.gridx = 1;
		gbc_jtf2.gridy = 1;
		jp.add(passwordField, gbc_jtf2);
		JLabel sex = new JLabel("性别：");
		changeColor(sex, -1);
		GridBagConstraints gbc_jl3 = new GridBagConstraints();
		gbc_jl3.anchor = GridBagConstraints.EAST;
		gbc_jl3.fill = GridBagConstraints.VERTICAL;
		gbc_jl3.gridx = 0;
		gbc_jl3.gridy = 2;
		jp.add(sex, gbc_jl3);

		getContentPane().add(jp);
		genderField = new JTextField(8);
		genderField.setText(adminEntity.isSex() ? "女" : "男");
		changeColor(genderField, 1);
		genderField.setCaretColor(Color.WHITE);
		GridBagConstraints gbc_jtf3 = new GridBagConstraints();
		gbc_jtf3.fill = GridBagConstraints.HORIZONTAL;
		gbc_jtf3.gridx = 1;
		gbc_jtf3.gridy = 2;
		jp.add(genderField, gbc_jtf3);

		JLabel quanxuan = new JLabel("权限：");
		changeColor(quanxuan, -1);
		GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.anchor = GridBagConstraints.EAST;
		gbc_label.fill = GridBagConstraints.VERTICAL;
		gbc_label.gridx = 0;
		gbc_label.gridy = 3;
		jp.add(quanxuan, gbc_label);

		textField = new JTextField(adminEntity.getLevel() == 0 ? "超级管理员" : "普通管理员");
		textField.setEnabled(false);
		changeColor(textField, 1);
		textField.setCaretColor(Color.WHITE);
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 1;
		gbc_textField.gridy = 3;
		jp.add(textField, gbc_textField);
		textField.setColumns(10);

		modifiy = new JButton("修改");
		modifiy.setFocusable(false);
		changeColor(modifiy, 1);
		GridBagConstraints gbc_button = new GridBagConstraints();
		gbc_button.fill = GridBagConstraints.HORIZONTAL;
		gbc_button.gridx = 1;
		gbc_button.gridy = 4;
		jp.add(modifiy, gbc_button);

		addMouseListener(adminEntity);
	}

	public void closeWindow() {
		this.dispose();
	}

	/**
	 * 添加鼠标监听
	 * 
	 * @param flag
	 */
	private void addMouseListener(AdminEntity adminEntity) {
		modifiy.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				IAdminService adminService = AdminServiceImpl.getProxyInstance();
				adminEntity.setName(nameField.getText());
				adminEntity.setPassword(passwordField.getText());
				adminEntity.setSex(genderField.getText() == "男" ? false : true);
				if (adminService.modify(adminEntity) == 1) {
					AdminEntity newEntity = adminService.findById(adminEntity.getId());
					if ("MainFrame".equals(framType)) {
						MainFrame.setAdminEntity(newEntity);
						MainFrame.setName(newEntity.getName());
					} else if ("MyFrame".equals(framType)) {
						MyFrame.setAdminEntity(newEntity);
						MyFrame.setName(newEntity.getName());
					}
					closeWindow();
				}
			}
		});
	}

	/**
	 * 改变组件的背景颜色和字体颜色
	 * 
	 * @param compoent
	 * @param flag
	 */
	private void changeColor(Component compoent, int flag) {
		if (flag == 1) {
			compoent.setBackground(new Color(44, 89, 110));
		} else if (flag == 2) {
			compoent.setBackground(new Color(25, 51, 64));
		}
		compoent.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		compoent.setForeground(Color.WHITE);
	}

	// public static void main(String[] args) {
	// new inforWindow("11", "11", true, 0,true).setVisible(true);
	// }
}